/*
 * FileName：SysDeptServiceImpl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.sys.service.impl;

import javax.annotation.Resource;

import com.smart.common.Constants;
import com.smart.common.util.SessionUtil;
import com.smart.common.util.StringUtil;
import com.smart.common.util.Util;
import com.smart.sys.entity.SysMenu;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import com.smart.sys.dao.SysDeptMapper;
import com.smart.sys.entity.SysDept;
import com.smart.sys.service.ISysDeptService;

/**
 * 部门
 *
 * @author hermit
 * @version 2.0
 * @date 2018-12-30 22:26:32
 */
@Service
@Transactional
public class SysDeptServiceImpl implements ISysDeptService {

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Override
    public SysDept queryObject(SysDept sysDept) throws Exception {
        return sysDeptMapper.queryObject(sysDept);
    }

    @Override
    public List<SysDept> queryList(SysDept sysDept) throws Exception {
        return sysDeptMapper.queryList(sysDept);
    }

    @Override
    public List<SysDept> queryListByPage(SysDept sysDept) throws Exception {
        return sysDeptMapper.queryListByPage(sysDept);
    }

    @Override
    public Integer queryCount(SysDept sysDept) throws Exception {
        Integer ct = sysDeptMapper.queryCount(sysDept);
        return ct == null ? 0 : ct;
    }

    @Override
    public Integer insert(SysDept sysDept) throws Exception {
        String maxIdByParent = sysDeptMapper.queryMaxIdByParent(sysDept.getParentId());
        sysDept.setDeptId(Util.getId(sysDept.getParentId(), maxIdByParent));
        sysDept.setCreateTime(new Date());
        sysDept.setUpdateTime(new Date());
        sysDept.setCreateUser(SessionUtil.getUserId());
        sysDept.setUpdateUser(SessionUtil.getUserId());
        sysDept.setLeafFlag(Constants.LEAF_YES);
        sysDept.setSystemFlag(Constants.SYSTEM_FLAG_NO);
        //父节点修改叶子节点
        sysDeptMapper.update(SysDept.builder().deptId(sysDept.getParentId()).leafFlag(Constants.LEAF_NO).build());
        return sysDeptMapper.insert(sysDept);
    }

    @Override
    public Integer update(SysDept sysDept) throws Exception {
        sysDept.setUpdateTime(new Date());
        sysDept.setUpdateUser(SessionUtil.getUserId());
        return sysDeptMapper.update(sysDept);
    }

    @Override
    public Integer delete(SysDept sysDept) throws Exception {
        sysDeptMapper.delete(sysDept);
        //更新父节点
        Integer count = sysDeptMapper.queryCount(SysDept.builder().parentId(sysDept.getParentId()).build());
        if (count == 0) {
            sysDeptMapper.update(SysDept.builder().deptId(sysDept.getParentId()).leafFlag(Constants.LEAF_YES).build());
        }
        return 1;
    }

    @Override
    public Integer deleteBatch(List<String> deptIds) throws Exception {
        return sysDeptMapper.deleteBatch(deptIds);
    }
}
